package com.book.management.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.book.common.annotation.ControllerEndpoint;
import com.book.common.annotation.Log;
import com.book.common.controller.BaseController;
import com.book.common.entity.FebsConstant;
import com.book.common.entity.FebsResponse;
import com.book.common.entity.QueryRequest;
import com.book.common.utils.FebsUtil;
import com.book.management.entity.Revocation;
import com.book.management.service.RevocationService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

/**
 * @Author: 何胤宏
 * @Description:  20173969
 */

@Controller
public class RevocationController extends BaseController {
    @Autowired
    RevocationService revocationService;

    @RequestMapping(FebsConstant.VIEW_PREFIX + "management/revocation")
    public String maBackIndex() {
        return FebsUtil.view("management/revocation/revocationList");
    }

    @GetMapping("revocation")
    @ResponseBody
    @RequiresPermissions("revocation:view")
    public FebsResponse findRevocation(Revocation revocation) {
        return new FebsResponse().success().data(revocationService.RevocationList(revocation));
    }

    @Log("访问退书统计")
    @GetMapping("revocation/list")
    @ResponseBody
    @RequiresPermissions("revocation:view")
    public FebsResponse revocationList(QueryRequest request, Revocation revocation) {
        Map<String, Object> dataTable = getDataTable(this.revocationService.RevocationPage(request, revocation));
        return new FebsResponse().success().data(dataTable);
    }

    /**
     * @Date: 2021/3/21
     * @Description: 柱状图
     * @Param:revocation
     * @return:Set<Revocation>
     */
    @Log("访问退书统计图例")
    @GetMapping("revocationStat/year")
    @ResponseBody
    @ControllerEndpoint(operation = "柱状图成功渲染", exceptionMessage = "柱状图渲染失败")
    public FebsResponse getRevocationList(Revocation revocation) {
        List<Revocation> revocations = revocationService.RevocationList(revocation);
        List<Revocation> revocationList = new ArrayList<>();
        Set<Revocation> revocationSet = new HashSet<>();
        for (int i = 0; i < revocations.size(); i++) {
            QueryWrapper<Revocation> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("bookname", revocations.get(i).getBookname());
            List<Revocation> revocationList1 = revocationService.list(queryWrapper);
            if (revocationList1.size() > 1) {
                int total = 0;
                for (int l = 0; l < revocationList1.size(); l++) {
                    total = total + revocationList1.get(l).getNumber();
                }
                revocationList1.get(0).setNumber(total);
                revocationList.add(revocationList1.get(0));
            } else {
                revocationList.add(revocations.get(i));
            }
        }
        for (int i = 0; i < revocationList.size(); i++) {
            revocationSet.add(revocationList.get(i));
        }
        return new FebsResponse().success().data(revocationSet);
    }
}
